package com.qeng.aiwanba.util;

import android.content.Context;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;

import cn.idianyun.streaming.DianyunSdk;
import cn.idianyun.streaming.listener.DYSdkListener;

/**
 * Created by lixy on 2017/10/11.
 */

public class MyDyOnlinePlayListener implements DYSdkListener {
    private WeakReference<Context> weakContext;
    private Context context;

    public MyDyOnlinePlayListener(Context context) {
        this.context = context;
        this.weakContext = new WeakReference<Context>(context);
    }

    @Override
    public void onStart() {
        DebugFlag.logVerbose(getClass().getSimpleName(),"试玩开始");
    }

    @Override
    public void onStop() {
        DebugFlag.logVerbose(getClass().getSimpleName(),"试玩停止");
        JSONObject object = new JSONObject();
        if(Constants.onlinePlayApp != null) {
            try {
                object.put("packageName", Constants.onlinePlayApp.getPackageName());
                object.put("data", Constants.onlinePlayApp.getData());
                object.put("url", Constants.onlinePlayApp.getDownloadUrl());
                object.put("id", Constants.onlinePlayApp.getSwId());
                object.put("playtime", DianyunSdk.getInstance().getPlayDuration());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        Constants.onlinePlayApp = null;
        Log.e("tag","callback:"+object.toString());
        Constants.topFrgment.noticeOnlinePlayEnd(object.toString());
    }

    @Override
    public boolean onDownloadClick(String s) {
        DebugFlag.logVerbose(getClass().getSimpleName(),"点击了试玩下载");
        if(Constants.onlinePlayApp != null && !Util.isEmpty(Constants.onlinePlayApp.getDownloadUrl())){
            DownloadUtil.addDownloadInfo(context,Constants.onlinePlayApp,~0);
        }
        return true;
    }


}
